package etxt2db.features;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import edu.cmu.minorthird.text.Annotator;
import edu.cmu.minorthird.text.BasicTextLabels;
import edu.cmu.minorthird.text.MonotonicTextLabels;
import edu.cmu.minorthird.text.Span;
import edu.cmu.minorthird.text.TextLabels;
import etxt2db.api.ClassificationModel;
import etxt2db.corpus.CompleteTextLabels;

public class ClassificationModelFeatureClassifier extends CharacterFeatureClassifier implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8524331389177255633L;
	private transient TextLabels annotatedDocument = null;
	ClassificationModel an;
	String name;
	
	public ClassificationModelFeatureClassifier(ClassificationModel annotator,String name) {
		an = annotator;
		this.name = name;
	}
	
	@Override
	public List<String> emit(String text) {
		if(annotatedDocument == null || !(annotatedDocument.getTextBase().equals(corpus.getTextBase()))){
			MonotonicTextLabels cleanCopy = new CompleteTextLabels(corpus.getTextBase());
			annotatedDocument = an.annotatedCopy(cleanCopy);
		}
		
		List<String> result = new ArrayList<String>();
		
		for(String type : annotatedDocument.getTypes()){
			Set<Span> annotatedSpans = annotatedDocument.getTypeSet(type, span.getDocumentId());
			for(Span span : annotatedSpans){
				if(span.contains(this.span)){
					result.add("AnnotationModel_" + name +"=" + type);
				}
			}
		}

		return result;
	}
}
